package com.fuluwa.interfaces.security.web;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fuluwa.domain.security.service.ShiroDbRealm.ShiroUser;
import com.fuluwa.domain.usercenter.model.Contact;
import com.fuluwa.domain.usercenter.model.UserBasicInfo;
import com.fuluwa.infra.context.UserUtils;
import com.fuluwa.infra.exception.PersistenceException;
import com.fuluwa.infra.web.AbstractDomainController;
import com.fuluwa.infra.web.Result;
import com.fuluwa.interfaces.security.facade.UserDTO;
import com.fuluwa.interfaces.security.facade.UserServiceFacade;

@Controller
@RequestMapping(value = "/user")
public class UserController extends AbstractDomainController {
	@Autowired
	private UserServiceFacade userServiceFacade;

	@RequestMapping(value = "check", method = RequestMethod.GET)
	@ResponseBody
	public Result check(@RequestParam String loginName) {
		logger.debug("request check(GET):" + loginName);
		boolean result = userServiceFacade.checkUserByLoginName(loginName);
		return result ? getSuccessResult() : getFailResult("User not exists.");
	}

	@RequestMapping(value = "register", method = RequestMethod.POST)
	@ResponseBody
	public Result registerUser(@RequestBody UserDTO userDTO) {
		logger.debug("request registerUser(POST):" + userDTO);
		try {
			userServiceFacade.registerUser(userDTO);
		} catch (Exception e) {
			logger.error("error", e);
			return getFailResult("User Register Failed:" + e.getMessage());
		}
		return getSuccessResult();
	}
	@RequestMapping(value = "updateContact", method = RequestMethod.POST)
	@ResponseBody
	public Result updateContact(@RequestBody Contact contact) {
		logger.debug("request updateContact(POST):" + contact);
		try {
			String loginName = UserUtils.getCurrentUserName();
			String userNo = userServiceFacade.getUserNo(loginName);
			userServiceFacade.updateContact(userNo,contact);
		} catch (Exception e) {
			logger.error("error", e);
			return getFailResult("updateContact Failed:" + e.getMessage());
		}
		return getSuccessResult();
	}
	@RequestMapping(value = "updateBasic", method = RequestMethod.POST)
	@ResponseBody
	public Result updateBasic(@RequestBody UserBasicInfo userBasicInfo) {
		logger.debug("request updateBasic(POST):" + userBasicInfo);
		try {
			String loginName = UserUtils.getCurrentUserName();
			String userNo = userServiceFacade.getUserNo(loginName);
			userServiceFacade.updateUserBasicInfo(userNo, userBasicInfo);
		} catch (Exception e) {
			logger.error("error", e);
			return getFailResult("updateBasic Failed:" + e.getMessage());
		}
		return getSuccessResult();
	}

	@RequestMapping(value = "changepassword", method = RequestMethod.POST)
	@ResponseBody
	public Result changepassword(@RequestParam String oldPassword, @RequestParam String newPassword) {
		logger.debug("request changepassword(POST)");
		try {
			ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			userServiceFacade.changeUserPassword(user.loginName, oldPassword, newPassword);
		} catch (Exception e) {
			logger.error("error", e);
			return getFailResult("User Change Password Failed:" + e.getMessage());
		}
		return getSuccessResult();
	}
}
